﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace SurvivalStrategy.WorldData.Colliders
{
    public class RectangleCollider : Collider
    {
        public Rectangle rectangle { get; internal set; }

        public RectangleCollider(Vector2 position, Vector2 size)
        {
            int x = (int)(position.X);
            int y = (int)(position.Y);
            rectangle = new Rectangle(x, y, (int)size.X, (int)size.Y);
        }

        public RectangleCollider(int x, int y, int width, int height)
        {
            rectangle = new Rectangle(x, y, width, height);
        }

        public override void SetOwner(WorldElement owner)
        {
            base.SetOwner(owner);
            int x = (int)(owner.Position.X + rectangle.X);
            int y = (int)(owner.Position.Z + rectangle.Y);
            rectangle = new Rectangle(x, y, rectangle.Width, rectangle.Height);
        }

        //Metoda zamieniajaca string na collider odpowiedniego typu
        public static RectangleCollider Parse(string[] values)
        {
            RectangleCollider result;
            try
            {
                int x = int.Parse(values[0]);
                int y = int.Parse(values[1]);
                int w = int.Parse(values[2]);
                int h = int.Parse(values[3]);
                result = new RectangleCollider(x, y, w, h);

            }
            catch { throw new Exception("Nieprawidłowy format wejscia."); }

            return result;
        }

        //Metoda przedstawia parametry danego collidera w formie stringa
        public override string ParseToString()
        {
            string result = rectangle.X + " " + rectangle.Y + " " + rectangle.Width + " " + rectangle.Height;
            return result;
        }

        public override bool CheckCollision(Collider other)
        {
            bool result = false;
            if (other is RectangleCollider)
            {
                result = rectangle.Intersects(((RectangleCollider)other).rectangle);
            }
            if (result)
                return true;
            return result;
        }
    }
}
